import { Component, OnInit } from '@angular/core';
import { AppVersionService } from './app-version.service';
import { PageEntity } from '../../../core/domain/page.entity';
import { StatusEnum } from '../../../core/enum/status.enum';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { NzMessageService, UploadFile } from 'ng-zorro-antd';
import { HTTP_BASE_URL, SERVERS_NAME } from '../../../config/config';
import { StorageUtil } from '../../../utils/storage.util';
import { SourceServer } from '../../../common/source.server';

@Component({
  selector: 'app-app-version',
  templateUrl: './app-version.component.html',
  providers: [AppVersionService, SourceServer]
})
export class AppVersionComponent implements OnInit {
  allList = [];
  isAdd = false;
  isEdit = false;
  isVisible = false;
  pageInfo: PageEntity = new PageEntity();
  param: VersionParam = new VersionParam();;
  validateForm: FormGroup;

  // 文件相关
  fileList: Array<any> = [];
  uploadUrl = `${SERVERS_NAME.UPLOAD_URL}`;
  headerInfo = null;

  constructor(private http: AppVersionService,
    private fb: FormBuilder,
    private sourceService: SourceServer,
    private message: NzMessageService) {
  }

  ngOnInit() {
    this.headerInfo = StorageUtil.getFileHeader();
    this.page();
    this.initForm();
  }

  initForm(): void {
    this.validateForm = this.fb.group({
      appName: [this.param.appName, [Validators.required]],
      versionId: [this.param.versionId, [Validators.required]],
      versionRemark: [this.param.versionRemark, [Validators.required]],
      autoUpdate: [this.param.autoUpdate = 1, [Validators.required]],
      appSourceId: [this.param.appSourceId = '1', [Validators.required]]
    });
  }

  /**
   * 分页查询
   */
  page(): void {
    this.http.page(this.pageInfo).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.allList = res.datas.data;
        this.pageInfo.totalRecords = res.datas.totalRecords;
      }
    });
  }

  // 改变是否强制更新版本
  changeStatus(data) {
    this.http.change({ id: data.id }).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
      }
    });
  }

  /**
   * 新增
   */
  clickAdd() {
    this.isAdd = true;
    this.isEdit = false;
    this.isVisible = true;
    this.param = new VersionParam();
    this.param.autoUpdate = 1;
    this.fileList = [];
  }

  /**
   * 编辑
   * @param data:数据
   */
  edit(data: any) {
    this.isAdd = false;
    this.isVisible = true;
    this.isEdit = true;
    this.param = data;
  }


  /**
   * 删除
   * @param id：数据id
   */
  delete(id) {
    this.http.delete(id).subscribe(res => {
      if (res.status === StatusEnum.SUCCESS) {
        this.message.success(res.message);
        this.page();
      }
    });
  }


  /**
   * 附件上传回调
   * @param event 上传回调信息
   */
  getUploadInfo(event) {
    const { file, fileList, type } = event;
    console.log(event)
    if (type === 'success') {
      const { datas, status } = file.response;
      if (status === StatusEnum.SUCCESS) {
        this.fileList = fileList;
        this.param.appSourceId = file.response.datas.id;
      }
    }
  }


  /**
   * 保存
   */
  submit() {
    if (this.isEdit) {
      this.http.update(this.param).subscribe(res => {
        if (res.status === StatusEnum.SUCCESS) {
          this.isVisible = false;
          this.message.success(res.message);
          this.page();
        } else {
          this.message.error(res.message);
        }
      });
    } else {
      this.http.create(this.param).subscribe(res => {
        if (res.status === StatusEnum.SUCCESS) {
          this.isVisible = false;
          this.message.success(res.message);
          this.page();
        } else {
          this.message.error(res.message);
        }
      });
    }
  }
}
class VersionParam {
  appName: string;
  appSourceId: string;
  autoUpdate: number;
  id: string;
  versionRemark: string;
  versionId: string;
}

